package com.jbm.jbmsupplier.util.abstracts;


import com.jbm.jbmsupplier.util.Interface;

/**
 * Created by 家帮忙 on 2017/1/9.
 */

public abstract class SendMessageInterfase {

//    private static String mServerIp = "app.jiabangmang.com";
//    private static String mServerIp = "192.168.0.36";
    private static String mServerIp = "192.168.0.157";
    private static int mSockPort = 1902;
    private static int mTimeOut = 50000;
    public  static   Interface gSocket = new Interface();
    protected abstract void RunContext();

    protected  SendMessageInterfase my = null;
    protected  static boolean mIsConnect = false;

    public SendMessageInterfase()
    {
        my = this;
    }

    protected void Run()
    {
        new Thread(SocketRunable).start();
    }
    private static void Connect()
    {
        if(IsConnect())
            return;

        String result = gSocket.Connect(mServerIp, mSockPort, mTimeOut,true);
        if(result.equals("OK"))
        {
            mIsConnect = true;
        }
    }

    public static  boolean IsConnect()
    {
        return mIsConnect;
    }

    private  Runnable SocketRunable = new Runnable(){
        @Override
        public void run()
        {

            try
            {
                if(!IsConnect())
                {
                    Connect();
                }
                if(IsConnect())
                {
                    my.RunContext();
                }
            } catch (Exception e)
            {
                my.RunContext();
                e.printStackTrace();
            } finally
            {
            }
        }
    };

}
